/**
 * 项目中会用的一系列的工具方法
 */
uni.utils = {
  /**
   * 用户反馈（轻提示）
   * @param {string} title 提示文字内容
   * @param {string} icon 提示图标类型
   */
  toast(title = '数据加载失败！', icon = 'none') {
    uni.showToast({
      title,
      icon,
      mask: true,
      duration: 2000
    })
  },
  SetPoint(arr, level) {
    const levelAWidth = 50
    const levelBWidth = 60
    const levelCWidth = 70
    const levelAHeight = 80
    const levelBHeight = 260
    const levelCHeight = 450
    const XspaceA = 180
    const XspaceB = 200
    const XspaceC = 400
    let width = 0
    let height = 0
    let xSpace = 0
    if (level == 1) {
      width = levelAWidth
      height = levelAHeight
      xSpace = XspaceA
    }
    if (level == 2) {
      width = levelBWidth
      height = levelBHeight
      xSpace = XspaceB
    }
    if (level == 3) {
      width = levelCWidth
      height = levelCHeight
      xSpace = XspaceC
    }

    let addX = 0 // 递增X

    return arr.map((res) => {
      let randomNumberX = uni.utils.getRandomNumber(30, 120)
      // let randomNumberX = 0
      let randomNumberY = uni.utils.getRandomNumber(50, 150)
      const A = [addX + randomNumberX, randomNumberY + height] //x,y 左上
      const B = [A[0] + width, A[1]] // 右上
      const C = [A[0], A[1] + width] // 左下
      const D = [A[0] + width, A[1] + width] // 右上
      const points = [A, B, C, D]

      addX = addX + xSpace
      console.log(points)
      return Object.assign({}, res, {
        show: true,
        width: width,
        points: points,
      })
    })
  },
  getRandomNumber(min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min
  },
  isPointInPolygon(point, polygon) {
    var x = point[0],
      y = point[1]
    var inside = false
    for (var i = 0, j = polygon.length - 1; i < polygon.length; j = i++) {
      var xi = polygon[i][0],
        yi = polygon[i][1]
      var xj = polygon[j][0],
        yj = polygon[j][1]

      var intersect =
        yi > y != yj > y && x < ((xj - xi) * (y - yi)) / (yj - yi) + xi
      if (intersect) inside = !inside
    }
    return inside
  }
}